package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import static com.sky.constant.MessageConstant.CATEGORY_BE_RELATED_BY_DISH;
import static com.sky.constant.MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL;
import static com.sky.constant.StatusConstant.DISABLE;

/**
 *
 * 分类管理实现类
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分页管理分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //分页查询sql语句：select * from sky_take_out.category where name = ? ,type = ?,limit ?,?
        //1.分页查询
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        //2.获取分页查询结果
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        //3.封装返回结果

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 启用禁用分类
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //修改分类的sql:update category set status = ? ,updateTime = ? ,updateUser = ? where id = ?
        Category category = Category.builder()
                .status(status)
                .id(id)
                //.updateTime(LocalDateTime.now())
                //.updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.update(category);
        log.info("修改分类状态成功");

    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = Category.builder()
                .id(categoryDTO.getId())
                .name(categoryDTO.getName())
                .sort(categoryDTO.getSort())
                //.updateTime(LocalDateTime.now())
                //.updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.update(category);

    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void save(CategoryDTO categoryDTO) {

        //新增分类，分类状态默认禁用
        Category category = Category.builder()
                .type(categoryDTO.getType())
                .name(categoryDTO.getName())
                .sort(categoryDTO.getSort())
                .status(DISABLE)
                //.createTime(LocalDateTime.now())
                //.createUser(BaseContext.getCurrentId())
                //.updateTime(LocalDateTime.now())
                //.updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.save(category);
        log.info("新增分类成功");

    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //删除分类，sql语句：delete from category where id = ?
        //1.分类是否关联了菜品
        Integer countByCategoryId = dishMapper.countByCategoryId(id);
        if (countByCategoryId > 0){
            //关联了菜品，抛出异常
            throw new DeletionNotAllowedException(CATEGORY_BE_RELATED_BY_DISH);
        }

        //2.分类是否关联了套餐
        Integer countByCategoryId1 = setmealMapper.countByCategoryId(id);
        if (countByCategoryId1 > 0){
            //关联了套餐，抛出异常
            throw new DeletionNotAllowedException(CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteById(id);
        log.info("删除分类成功");
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List list(Integer type) {
        List<Category> list = categoryMapper.list(type);
        return list;
    }
}
